package com.maibow.util;

import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

public class HttpClientConnectUtil {
    protected static final Logger logger = Logger.getLogger(HttpClientConnectUtil.class);

    /**
     * 获取String类型返回内容
     * 
     * @param url
     * @return
     * @throws Exception
     */
    public static int CONNECTION_TIMEOUT = 20000;

    /**
     * 服务与后台servlet通讯,加密方法
     * 
     * @param url
     *            通讯地址
     * @param commmentTimeInfo
     *            携带的参数
     * @return 处理结果，“”表示连接不成功
     * @throws Exception
     */
    public static String getFromCipherConnectionSession(String url, String content, String sessionID, boolean ciphere, boolean compress) {
        HttpEntity ent = null;
        if (ciphere) {
            try {
                content = CipherTool.getCipherString(content);
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }
        }
        if (compress) {
            content = ZipUtils.gzip(content);
        }
        HttpPost httpRequest = new HttpPost(url);
        if (sessionID != null) {
            httpRequest.addHeader("Cookie", "J_SESSIONID=" + sessionID + ";" + "JSESSIONID=" + sessionID);
        }
        HttpResponse httpResponse = null;
        // 绑定到请求 Entry
        StringEntity se = null;
        se = new StringEntity(content, "UTF-8");
        // se.setContentType("");
        se.setContentEncoding("UTF-8");
        httpRequest.setEntity(se);
        DefaultHttpClient client = new DefaultHttpClient();
        HttpParams params = client.getParams();
        HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT);
        int t = 0;
        while (t < 2 && ent == null) {
            try {
                httpResponse = client.execute(httpRequest);
                ent = httpResponse.getEntity();
                // 处理从服务器短来的数据
                if (ent != null) {
                    String ret = EntityUtils.toString(ent);
                    if (compress) {
                        ret = ZipUtils.gunzip(ret);
                    }
                    if (ciphere) {
                        ret = CipherTool.getOriginString(ret);
                    }
                    return ret;
                } else {
                    t++;
                    Thread.sleep(500);
                }
            } catch (HttpHostConnectException e) {
                e.printStackTrace();
                ent = null;
                t++;
            } catch (Exception e) {
                e.printStackTrace();
                ent = null;
                t++;
            }
        }
        return "";
    }

    public static void main(String[] args) {
        // String str =
        // "ewoJInNpZ25hdHVyZSIgPSAiQW16Mlc3TTIrTExldS9OTU93ZmJROVlYY01oaHdaOWR1N3JnVGIvQzVtVHpXdEZhNmtPdlFzbnhGSXBqS1dqVCtnV1FWR3FmWllhVDFUYlkrcHNHL215NGxFenp0K3JJaUpUSXZ3MWwwb1FPMFNQcjh5Zk5mK2RtMGtMekN1OFpMUFBRVzBBNmxUQ0RJQ1RaSVVmNDFRUmsxRUNFbEVUMi9YN2ZqVUhuZDZhZ0FBQURWekNDQTFNd2dnSTdvQU1DQVFJQ0NCdXA0K1BBaG0vTE1BMEdDU3FHU0liM0RRRUJCUVVBTUg4eEN6QUpCZ05WQkFZVEFsVlRNUk13RVFZRFZRUUtEQXBCY0hCc1pTQkpibU11TVNZd0pBWURWUVFMREIxQmNIQnNaU0JEWlhKMGFXWnBZMkYwYVc5dUlFRjFkR2h2Y21sMGVURXpNREVHQTFVRUF3d3FRWEJ3YkdVZ2FWUjFibVZ6SUZOMGIzSmxJRU5sY25ScFptbGpZWFJwYjI0Z1FYVjBhRzl5YVhSNU1CNFhEVEUwTURZd056QXdNREl5TVZvWERURTJNRFV4T0RFNE16RXpNRm93WkRFak1DRUdBMVVFQXd3YVVIVnlZMmhoYzJWU1pXTmxhWEIwUTJWeWRHbG1hV05oZEdVeEd6QVpCZ05WQkFzTUVrRndjR3hsSUdsVWRXNWxjeUJUZEc5eVpURVRNQkVHQTFVRUNnd0tRWEJ3YkdVZ1NXNWpMakVMTUFrR0ExVUVCaE1DVlZNd2daOHdEUVlKS29aSWh2Y05BUUVCQlFBRGdZMEFNSUdKQW9HQkFNbVRFdUxnamltTHdSSn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